<?php


namespace app\admin\service;

use app\admin\model\Dict;
use app\admin\model\DictData;

/**
 * 字典服务
 * @package app\admin\service
 */
class DictService
{
    public static function get($type)
    {
        return Dict::where('type', $type)->with('list')->find();
    }

    public static function getData($type, $value)
    {
        $row = self::get($type)->toArray();
        // die(var_dump($row));
        foreach ($row['list'] as $datum) {
            if ($datum['value'] == $value) {
                return $datum['title'];
            }
        }
        return '';
    }

    public static function getAll($is_reverse=false)
    {

        $dict_list = Dict::where('status', 1)->column('id,title,type', 'id');
        // die(var_dump($dict_list));
        $data_list = DictData::where('status', 1)->order(['sort' => 'desc'])->column('title,value,pid', 'id');
        // die(var_dump($data_list));
        $res = [];
        foreach ($data_list as $item) {
$p=ArrayService::getValue($dict_list,$item['pid']);
if(empty($p)){
    continue;
}

            if($is_reverse){

            $res[$p['type']][$item['title']] = $item['value'];
            }else{

            $res[$p['type']][$item['value']] = $item['title'];
            }
        }
        return $res;

    }
}
